﻿using Futaba.SBus.Telemetry.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Futaba.SBus.Telemetry
{

	/// <summary>
	/// The context used by the <see cref="SlotValueParser"/> to be able to parse the
	/// <see cref="SlotValueParserContext.Data"/>
	/// </summary>
	public class SlotValueParserContext
	{
		/// <summary>
		/// The amount of times this slot has been processed
		/// </summary>
		public int ProcessedCount { get; set; }
		/// <summary>
		/// The amount of times a value of 0 has been parsed
		/// </summary>
		public int ZeroValueCount { get; set; }
		/// <summary>
		/// The amount of consecutive times the parsed value was 0
		/// </summary>
		public int ConsecutiveZeroValueCount { get; set; }
		/// <summary>
		/// The ratio of 0 to non-zero values. if it's approx. 0.5, then the packets only carry
		/// "real" values every other packet
		/// </summary>
		public double ZeroValueRatio { get { return ((double)ZeroValueCount / (double)ProcessedCount); } }
		/// <summary>
		/// The slot's current value (used for BogusValueDetection)
		/// </summary>
		public double? CurrentValue { get; set; }
		/// <summary>
		/// These settings influence parsing
		/// </summary>
		public SensorValueParsingFlags ParsingFlags { get; set; }
		/// <summary>
		/// The parsed value is multiplied by this value (some sensors require this)
		/// </summary>
		public double ValueMultiplicator { get; set; }
		/// <summary>
		/// The data contains 2 payload-bytes per slot to be parsed
		/// </summary>
		/// <seealso cref="SensorParameter.ConsumesSlots"/>
		public byte[] Data { get; set; }

		/// <summary>
		/// Last bogus value is kept, so new value candidates are checked against it, instead of the last
		/// value (on rapid growing values)
		/// </summary>
		public double LastBogusValue { get; set; }

		/// <summary>
		/// The Slot being processed
		/// </summary>
		public SlotIdType SlotId { get; set; }

		/// <summary>
		/// Updates the context from the <see cref="Slot">slot's</see> current values
		/// </summary>
		/// <param name="slot"></param>
		public void UpdateFromSlot(ISlot slot)
		{
			CurrentValue = slot.CurrentValue;
			ParsingFlags = slot.Parameter.ParsingFlags;
			ValueMultiplicator = slot.ValueMultiplyer != 0 ? slot.ValueMultiplyer : slot.Parameter.ValueMultiplyer;
			SlotId = slot.SlotId;
		}
	}
}
